import DestructibleFacility from "../../facility/DestructibleFacility";
import Mine from "../../facility/pulbicFacility/Mine";
import MinerHome from "../../funcObj/interface/MinerHome";
import { FacilityType } from "../../info/GameEnum";
import { Global } from "../../info/Global";
import GameManager from "../../manager/GameManager";
import { Location } from "../../info/Predefine";
import Player from "../../player/Player";
import Flyer from "../Flyer";
import NodePoolManager from "../../manager/NodePoolManager";

const { ccclass, property } = cc._decorator;

/**
 * 矿工，寻找周围的矿，找到后移动去挖矿，挖矿2秒后运回所属基地
 */
@ccclass
export default class Miner extends Flyer {

    @property(cc.Node)
    minerCrystal: cc.Node = null;

    @property(cc.Animation)
    miningAni: cc.Animation = null;

    /**
     * 矿工之家
     */
    base: DestructibleFacility & MinerHome;


    /**
     * 携带数量
     */
    carryCount: number;

    init(base: DestructibleFacility) {
        super.init(base)
        let level = this.player.minerSpeedLevel;
        this.moveComp.moveSpeed = this.flyerInfo.移动速度 + Global.FacilityInfo[FacilityType.主基地].矿工速度.每等级加成[level] * 100
        this.player.node.on(Player.EventType.更新矿工速度等级, this.updateMoveSpeed, this);

        this.carryCount = 0;
        this.miningAni.node.active = false;
        this.minerCrystal.active = false;
        //初始寻找矿地并出发
        this.findDiamondMine();
    }

    protected onDisable(): void {
        this.player.node.targetOff(this);
    }

    dispatch(target) { }

    updateMoveSpeed() {
        //实时更新移动速度
        this.moveComp.moveSpeed = this.flyerInfo.移动速度 + Global.FacilityInfo[FacilityType.主基地].矿工速度.每等级加成[this.player.minerSpeedLevel] * 100
    }

    /**
     * 
     * @returns 依据当前位置寻找最近的矿地
     */
    findDiamondMine() {
        let domainLocation = GameManager.instance.getMapLocation(this.node.position);
        let closestMineLocation = GameManager.instance.findNearestOne(domainLocation, (location: Location) => {
            let temp = GameManager.instance.getFacilityByLocation(location);
            return temp && temp.facilityType == FacilityType.晶矿 && (<Mine>temp).curCount > 0 && (<Mine>temp).takeMiners.size < 2;
        });

        let mine = GameManager.instance.getFacilityByLocation(closestMineLocation) as Mine;
        if (mine) {
            this.moveComp.setTarget(mine.node.position, this.onArriveMine.bind(this));
        } else {
            //找不到矿地就消失吧！！！
            this.node.active = false;
        }
    }

    /**
     * 到达矿地
     */
    onArriveMine() {
        //如果有人，重新寻找
        let mine = GameManager.instance.getFacilityByPosition(this.node.position) as Mine;
        if (!mine || mine.facilityType != FacilityType.晶矿 || mine.takeMiners.size > 1) {
            return this.findDiamondMine();
        }

        mine.takeMiners.add(this);
        let miningCount = this.player.miningCount;
        this.carryCount = mine.loseCount(miningCount);
        //如果矿地空了，回基地重新寻找矿地
        this.miningAni.node.active = true;
        this.miningAni.play();
        this.miningAni.on(cc.Animation.EventType.FINISHED, () => {
            //回基地
            mine.takeMiners.delete(this);
            this.miningAni.node.active = false;
            this.minerCrystal.active = true;
            this.moveComp.setTarget(this.base.node.position, this.onArriveDomain.bind(this));
            this.miningAni.targetOff(this);
        }, this);


    }

    /**
     * 到达基地
     */
    onArriveDomain() {
        //增长矿石
        this.base.addMineCount(this.carryCount);
        this.carryCount = 0;
        this.minerCrystal.active = false;
        //重新寻找
        this.findDiamondMine();
    }

    die() {
        let mine = GameManager.instance.getFacilityByPosition(this.node.position) as Mine;
        if(mine && mine.facilityType == FacilityType.晶矿){
            mine.takeMiners.delete(this);
        }
        this.onArrive();
        this.miningAni.stop();
        this.miningAni.targetOff(this);
        NodePoolManager.instance.recycle(this.node);
    }

}
